package com.mayn.web.index;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.mayn.action.JdTradeAction;
import com.mayn.action.JdpTbTradeAction;
import com.mayn.common.TradeObject;
import com.mayn.jst.taobao.TaobaoOrderAction;
import com.mayn.util.JsonFormatUtil;

import net.sf.json.JSONObject;

/**
 * IndexController
 */
public class IndexController extends Controller {
	
	private static final Logger LOG = LoggerFactory.getLogger(IndexController.class);
	
	public void index() {
		renderText("This is a Jfinal web project");
	}
		
	public void api()
	{
		@SuppressWarnings("unused")
		String app_key = getPara("app_key");
		String method = getPara("method");
		String session = getPara("session");
		String tid = getPara("tid");
		String fields = getPara("fields");
		if(!method.equals("taobao.trade.fullinfo.get") && !method.equals("taobao.trade.get") ){
			String msg = "Error method : " + method ; 
			renderError(msg);
			return;
		}
		
		String getShopIdSql = "select SHOP_ID from shop where SHOP_API_ACCESS_TOKEN='" + session + "'";
		Record getShopIdRec = Db.findFirst(getShopIdSql);
		int shopId = 0;
		if(getShopIdRec!=null){
			shopId = getShopIdRec.getInt("SHOP_ID");
		}
		if(shopId == 0){
			JSONObject err_response = getErrResponse("15","Remote service error","isv.trade-not-exist","shopId= " + shopId + ",tid=" + tid + ",not exists!");
			renderJsonError(err_response);
			return;
		}
		TradeObject jdpTbTradeObject = new TradeObject();
		if(method.equals("taobao.trade.get")){
			if(fields.equals("status")){
				jdpTbTradeObject = JdpTbTradeAction.getTradeInfoByApi(shopId, tid);
			}else{
				jdpTbTradeObject = JdpTbTradeAction.getTradeInfo(shopId, tid);	
			}
		}
		if(method.equals("taobao.trade.fullinfo.get")){
			if(fields.equals("status")){
				jdpTbTradeObject = JdpTbTradeAction.getTradeFullInfoByApi(shopId, tid);
			}else{
				jdpTbTradeObject = JdpTbTradeAction.getTradeFullInfo(shopId, tid);
			}
		}
		if(jdpTbTradeObject.getRetCode()==1){
			String tradeFullInfo = jdpTbTradeObject.getTrade();
			LOG.debug(tradeFullInfo);
			renderJson(tradeFullInfo);
		}
		else{
			JSONObject err_response = getErrResponse("15","Remote service error","isv.trade-not-exist",jdpTbTradeObject.getRetMsg());
			renderJsonError(err_response);
			return;
		}
	}
	
	/*
	 * http://127.0.0.1:88/getTbTrade?tid=1662064806003922
	 * http://120.26.195.93/JST-API/getTbTrade?tid=1662064806003922
	 */
	public void getTbTrade(){
		if("GET".equals(getRequest().getMethod()))
		{
			String tid = "";
			int shopId = 0;
			if(getPara("tid")==null || getPara("tid").equals("")){
				String msg = "Error tid : " + tid ; 
				renderError(msg);
				return;
			}else{
				tid = getPara("tid");
			}
			if(getPara("shopId")==null || getPara("shopId").equals("")){
				@SuppressWarnings("unused")
				String msg = "Error shopId : " + shopId ; 
				//shopId非必填
			}else{
				shopId = Integer.parseInt(getPara("shopId"));
			}
			if(shopId >0){
				String tradeFullInfo = JdpTbTradeAction.getTradeByTid(shopId,tid);
				LOG.debug(tradeFullInfo);
				renderJson(tradeFullInfo);
			}else{
				String tradeFullInfo = JdpTbTradeAction.getTradeByTid(tid);
				LOG.debug(tradeFullInfo);
				renderJson(tradeFullInfo);
			}
		}
	}
	
	/*
	 * http://127.0.0.1:88/getTbTradeJson?tid=1662064806003922
	 * http://120.26.195.93/JST-API/getTbTradeJson?tid=1662064806003922
	 */
	public void getTbTradeJson(){
		if("GET".equals(getRequest().getMethod()))
		{
			String tid = "";
			if(getPara("tid")!=null && !getPara("tid").equals("")){
				tid = getPara("tid");
				String tradeFullInfo = JdpTbTradeAction.getTradeByTid(tid);
				LOG.debug(tradeFullInfo);
				String fmtString = JsonFormatUtil.formatJson(tradeFullInfo);
				renderText(fmtString);
			}
		}
	}
	
	public void getTbTradeJsonByApi(){
		if("GET".equals(getRequest().getMethod()))
		{
			String tid = "";
			int shopId = 0;
			if(getPara("tid")==null || getPara("tid").equals("")){
				String msg = "Error tid : " + tid ; 
				renderError(msg);
				return;
			}else{
				tid = getPara("tid");
			}
			if(getPara("shopId")==null || getPara("shopId").equals("")){
				String msg = "Error shopId : " + shopId ; 
				renderError(msg);
				return;
			}else{
				shopId = Integer.parseInt(getPara("shopId"));
			}
			TradeObject tradeObject = new TradeObject(); 
			tradeObject = JdpTbTradeAction.getTradeFullInfoByApi(shopId, tid);
			if(tradeObject.getRetCode()==1){
				renderJson(tradeObject.getTrade());
			}else{
				String msg = "淘宝订单不存在 : " + shopId + "--" +  tid; 
				renderError(msg);
				return;
			}
		}
	}
	
	
	public void getJdTrade(){
		if("GET".equals(getRequest().getMethod()))
		{
			String tid = "";
			int shopId = 0;
			if(getPara("tid")==null || getPara("tid").equals("")){
				String msg = "Error tid : " + tid ; 
				renderError(msg);
				return;
			}else{
				tid = getPara("tid");
			}
			if(getPara("shopId")==null || getPara("shopId").equals("")){
				String msg = "Error shopId : " + shopId ; 
				renderError(msg);
				return;
			}else{
				shopId = Integer.parseInt(getPara("shopId"));
			}
			TradeObject tradeObject = new TradeObject(); 
			tradeObject = JdTradeAction.getTradeInfo(shopId, tid);
			if(tradeObject.getRetCode()==1){
				renderJson(tradeObject.getTrade());
			}else{
				String msg = "京东订单不存在 : " + shopId + "--" +  tid; 
				renderError(msg);
				return;
			}
		}
	}
	
	/*
	 * http://127.0.0.1:88/getJdTradeJson?shopId=2&tid=11746294016
	 * http://120.26.195.93/JST-API/getJdTradeJson?shopId=2&tid=11746294016
	 */
	public void getJdTradeJson(){
		if("GET".equals(getRequest().getMethod()))
		{
			String tid = "";
			int shopId = 0;
			if(getPara("tid")==null || getPara("tid").equals("")){
				String msg = "Error tid : " + tid ; 
				renderError(msg);
				return;
			}else{
				tid = getPara("tid");
			}
			if(getPara("shopId")==null || getPara("shopId").equals("")){
				String msg = "Error shopId : " + shopId ; 
				renderError(msg);
				return;
			}else{
				shopId = Integer.parseInt(getPara("shopId"));
			}
			TradeObject tradeObject = new TradeObject(); 
			tradeObject = JdTradeAction.getTradeInfo(shopId, tid);
			if(tradeObject.getRetCode()==1){
				String fmtString = JsonFormatUtil.formatJson(tradeObject.getTrade());
				renderText(fmtString);
			}else{
				String msg = "京东订单不存在 : " + shopId + "--" +  tid; 
				renderError(msg);
				return;
			}
		}
	}
	
	
	/**
	 * 检查是否村淘订单
	 */
	public void checkCuntao(){
		if("GET".equals(getRequest().getMethod()))
		{
			String tid = "";
			int shopId = 0;
			if(getPara("tid")==null || getPara("tid").equals("")){
				String msg = "Error tid : " + tid ; 
				renderError(msg);
				return;
			}else{
				tid = getPara("tid");
			}
			if(getPara("shopId")==null || getPara("shopId").equals("")){
				String msg = "Error shopId : " + shopId ; 
				renderError(msg);
				return;
			}else{
				shopId = Integer.parseInt(getPara("shopId"));
			}
			Boolean c = TaobaoOrderAction.isCuntaoOrder(shopId, tid);
			JSONObject ret = new JSONObject();
			ret.put("isCuntao", c);
			renderJson(ret);
		}
	}
	
	public static JSONObject getErrResponse(String code,String msg,String sub_code,String sub_msg){
		JSONObject ret = new JSONObject();
		JSONObject error_response = new JSONObject();
		error_response.put("code", code);
		error_response.put("msg", msg);
		error_response.put("sub_code", sub_code);
		error_response.put("sub_msg", sub_msg);
		error_response.put("request_id", "");
		ret.put("error_response", error_response);
		return ret;
	}
	
	public void renderError(String msg){
		LOG.error(msg);
		renderJson(msg);
	}
	
	
	public void renderJsonError(JSONObject object){
		LOG.error(object.toString());
		renderJson(object);
	}
}





